/*
 ********************************************************************************
 * @FileName  : cfg_param.c
 * @CreateDate: 2021-8-10
 * @Author    : GaoQiu
 * @Copyright : Copyright(C) GaoQiu
 *              All Rights Reserved.
 ********************************************************************************
 * The information contained herein is confidential and proprietary property of
 * GaoQiu and is available under the terms of Commercial License Agreement
 * between GaoQiu and the licensee in separate contract or the terms described
 * here-in.
 *
 * This heading MUST NOT be removed from this file.
 *
 * Licensees are granted free, non-transferable use of the information in this
 * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
 *
 *******************************************************************************
 */
#include <string.h>
#include "cfg_param.h"
#include "utils/math.h"
#include "hal_flash.h"
#include "component/kv/kv_api.h"

char *configKey[] = {
	"bd_addr",
	"mac_filter_addr",
};

char *GetConfigName(uint8_t idx)
{
	return configKey[idx];
}

void GetConfigParam(uint8_t paramType, uint8_t *pParam, uint32_t len)
{
	switch(paramType)
	{
	case CFG_PARAM_MAC_ADDR:
	{
	#if 0
		uint32_t tmp = 0;
		//FLASH_ReadPage(MAC_ADDR_BASE, (uint8_t *)&tmp, 4);
		HalFlashRead(MAC_ADDR_BASE, (uint8_t *)&tmp, 4);
		if(tmp == 0xFFFFFFFF){
			PrandNums(pParam, len);
			pParam[len-1] |= 0xC0; // static device address;
			//FLASH_WritePage(MAC_ADDR_BASE, pParam, len);
			HalFlashWrite(MAC_ADDR_BASE, pParam, len);
		}else{
			//FLASH_ReadPage(MAC_ADDR_BASE, pParam, len);
			HalFlashRead(MAC_ADDR_BASE, pParam, len);
		}
	#else
		char *key = configKey[CFG_MAC_ADDR_IDX];
		uint32_t tmp = kv_get_item_ex(key, pParam, len);
		if(tmp == 0){
			PrandNums(pParam, len);
			pParam[len-1] |= 0xC0; // static device address;
			kv_set_item(key, pParam, len);
		}
	#endif
		break;
	}
	case CFG_PARAM_MAC_FILTER:
	{
	#if 0
		/* | MAC Filter Enable(1B) | MAC Filter Length(1B) |  MAC Address(6B) | */
		uint32_t tmp = 0;
		HalFlashRead(MAC_FILTER_BASE, (uint8_t *)&tmp, 4);
		if(tmp == 0xFFFFFFFF){
			pParam[0] = 0;
		}
		else{
			HalFlashRead(MAC_FILTER_BASE, pParam, 8);
		}
	#else
		/* | MAC Filter Enable(1B) | MAC Filter Length(1B) |  MAC Address(6B) | */
		char *key = configKey[CFG_MAC_FILTER_IDX];
		uint32_t res = kv_get_item_ex(key, pParam, len);
		if(res == 0){
			memset(pParam, 0, len);
		}
	#endif
		break;
	}
	default:
		break;
	}
}
